package cn.zzu.faceorgans;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.linchaolong.android.imagepicker.ImagePicker;


import java.net.URI;

import cn.zzu.faceorgans.faceAPI.FaceLandmark;
import cn.zzu.faceorgans.utils.Base64My;

public class GetFaceActivity extends AppCompatActivity implements View.OnClickListener{

    private ImagePicker imagePicker = new ImagePicker();


    Button take_photo; //照相按钮

    ImageView faceImage;    // 获得的相片

    public static final int TAKE_PHOTO = 1;
    private Uri imageURI;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_get_face);


        take_photo = findViewById(R.id.take_photo);
        faceImage = findViewById(R.id.face);

        take_photo.setOnClickListener(this);

        // 设置标题
        imagePicker.setTitle("设置头像");
        // 设置是否裁剪图片
        imagePicker.setCropImage(false);


    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.take_photo:
                startChooser();
                break;
        }
    }


    // 选择图片
    private void startChooser() {
        // 启动图片选择器
        imagePicker.startChooser(this, new ImagePicker.Callback() {
            // 选择图片回调
            @Override public void onPickImage(Uri imageUri) {
                Log.d("Uri","选择图片");
                String uri = imageUri.toString();
                Log.d("Uri", imageUri.toString());
                Uri u2 = Uri.parse(uri);
                Log.d("Uri2", u2.toString());

                Log.d("Uri3", URI.create(u2.toString()).getPath());

//                Log.d("UriBase64", Base64My.imageToBase64(URI.create(u2.toString()).getPath()));

                faceImage.setImageURI(u2);

                String imageBase = Base64My.imageToBase64(URI.create(u2.toString()).getPath());
                getFromBaidu(imageBase);

            }

            // 用户拒绝授权回调
            @Override public void onPermissionDenied(int requestCode, String[] permissions,
                                                     int[] grantResults) {

                Toast.makeText(GetFaceActivity.this, "必须授权", Toast.LENGTH_SHORT).show();
            }
        });
    }


    // 调用百度API获取关键点
    private void getFromBaidu(String imageBase64){
        MyThread myThread = new MyThread(imageBase64);
        myThread.start();
    }

    class MyThread extends Thread{
        private String imageBase;

        public MyThread(String imageBase){
            this.imageBase = imageBase;
        }

        @Override
        public void run() {
//            String result = FaceLandmark.faceLandmark(imageBase);

            FaceLandmark.faceLandmarkImportantPoint(imageBase);
        }
    }









    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        imagePicker.onActivityResult(this, requestCode, resultCode, data);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        imagePicker.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
    }
}